package admin

import (
	"exercise/application/yiran/app/libs"
	"exercise/application/yiran/app/libs/errors"
	"exercise/application/yiran/app/model_new/admin"
	"fmt"
	"github.com/gin-gonic/gin"
	"sync"
	"sync/atomic"
)

type indexController struct {
}

type Student struct {
	Username string `json:"username" form:"username" validate:"required" label:"用户名ss"`
	Age      int    `json:"age" form:"age" `
	Sex      int    `json:"sex" form:"sex"`
	Address  string `json:"address" form:"address"`
}

func NewStudent() *Student {
	return &Student{Sex: 1}
}

func putNum(list chan int, count int) {
	//全部写完成,关闭通道
	defer close(list)

	for i := 2; i <= count; i++ {
		fmt.Println("存入了：" + libs.Int2String(i))
		list <- i
	}
}
func primeNum(list chan int, primeList chan int) {
	defer close(primeList)
	for num := range list {
		var flag = true
		for i := 2; i < num; i++ {
			if num%i == 0 {
				flag = false
				break
			}
		}
		if flag {
			fmt.Printf("将素数：%v 放入通道中\n", num)
			primeList <- num //num是素数
		}
	}
}

func readNum(list chan int) {

	for {
		select {
		case val, ok := <-list:
			if !ok {
				// 通道已关闭并且没有更多数据
				fmt.Println("通道已关闭，没有更多数据")
				return
			}
			fmt.Printf("打印素数：%v \n", val)
		default:
			// 检查通道是否已被关闭
			if _, ok := <-list; !ok {
				fmt.Println("通道已关闭，没有更多数据")
				return
			}
			fmt.Println("44444444444444444444444444444444444：没有数据了")
			return
		}
	}
	//for val := range list {
	//	fmt.Printf("打印素数：%v \n", val)
	//}
}

// 定义一个全局变量用于存放初始化资源
var resource string

// 定义一个sync.Once变量
var once sync.Once

// 初始化次数
var initCount int

// 模拟资源初始化的函数
func initialize() {
	fmt.Println("Initializing resource...")
	resource = "Resource Initialized"
	initCount++
}

// Index 首页
func (ic *indexController) Index(c *gin.Context) {

	errors.ThrowError(errors.ErrorCode, "请求失败")

	adms := admin.NewAdminMenuModel().List()
	libs.Success(c, adms)
	return

	var wg sync.WaitGroup
	var count int32 = 0

	// 启动多个goroutine来增加计数器
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			for j := 0; j < 5; j++ {
				// 使用atomic.AddInt32来原子地增加计数器
				atomic.AddInt32(&count, 1)
			}
		}()
	}

	// 等待所有goroutine完成
	wg.Wait()

	// 使用atomic.LoadInt32来原子地读取计数器
	finalCount := atomic.LoadInt32(&count)
	fmt.Println("Final count:", finalCount)

	// 尝试使用atomic.CompareAndSwapInt32来条件性地更新计数器
	if atomic.CompareAndSwapInt32(&count, finalCount, 100) {
		fmt.Println("Count was", finalCount, ", updated count to 100")
	} else {
		fmt.Println("Failed to update count")
	}

	// 再次读取并打印计数器的值
	updatedCount := atomic.LoadInt32(&count)
	fmt.Println("Updated count:", updatedCount)
}

// checkUnique 唯一性校验,mysql唯一索引
// 1.批量插入的时候 唯一索引报错，后面的就不执行了，导致批量插入的结果是数据少
// 2.现将mysql符合条件的放到一个map中，再将数据与map中的比较
func (ic *indexController) checkUnique() ([]map[string]string, []map[string]string) {
	original := make(map[string]bool, 0)

	original["202312112121"] = true
	original["202312122122"] = true

	list := make([]map[string]string, 0)
	c1 := make(map[string]string, 0)
	c1["code"] = "2022"
	c1["name"] = "瑶海"
	c1["date"] = "202312112121"

	c2 := make(map[string]string, 0)
	c2["code"] = "2022"
	c2["name"] = "新站"
	c2["date"] = "202312122124"

	c3 := make(map[string]string, 0)
	c3["code"] = "2021"
	c3["name"] = "新站"
	c3["date"] = "202312122123"

	list = append(list, c1, c2, c3)

	unique := make([]map[string]string, 0)
	noUnique := make([]map[string]string, 0)
	for _, v := range list {
		if original[v["date"]] {
			noUnique = append(noUnique, v)
		} else {
			unique = append(unique, v)
		}
	}

	return unique, noUnique

}

// AscendingDimension 升维,将指定元素相同的切边放到一个下表下面
func (ic *indexController) AscendingDimension() map[string][]map[string]string {
	cityList := make([]map[string]string, 0)
	c1 := make(map[string]string, 0)
	c1["code"] = "2022"
	c1["name"] = "瑶海"

	c2 := make(map[string]string, 0)
	c2["code"] = "2022"
	c2["name"] = "新站"

	c3 := make(map[string]string, 0)
	c3["code"] = "2021"
	c3["name"] = "新站"

	cityList = append(cityList, c1, c2, c3)

	list := make(map[string][]map[string]string)
	for _, c := range cityList {
		list[c["code"]] = append(list[c["code"]], c)
	}
	return list
}

func (ic *indexController) List(ctx *gin.Context) {
	schoolId, _ := ctx.Get("schoolId")

	libs.Success(ctx, schoolId)
}

func NewIndexController() *indexController {
	return &indexController{}
}
